﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ComicViewer.IManhua;

namespace ComicViewer
{
    /// <summary>
    /// Interaction logic for PageFunction1.xaml
    /// </summary>
    public partial class HomePage : PageFunction<String>
    {
        public HomePage()
        {
            InitializeComponent();

            this.Loaded += new RoutedEventHandler(HomePage_Loaded);
        }

        void HomePage_Loaded(object sender, RoutedEventArgs e)
        {
            //http://www.imanhua.com/
            ListBoxCategories.ItemsSource = new IManhuaProxy().GetCategories();
        }

        private void ListBoxCategories_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ComicCategory category = e.AddedItems[0] as ComicCategory;
            if (category.Books.Count == 0)
            {
                ListBoxBooks.ItemsSource = new IManhuaProxy().GetBooks(category);
            }
            else
            {
                ListBoxBooks.ItemsSource = category.Books;
            }
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            ComicBook book = (sender as Button).DataContext as ComicBook;
            Application.Current.Properties["Book"] = book;
            NavigationService.Navigate(new BookDetailPage(), book);
        }

        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(editKeyword.Text))
            {
                return;
            }
            ListBoxBooks.ItemsSource = new IManhuaProxy().SearchBooks(editKeyword.Text.Trim());
        }
    }
}
